package com.power.sifa.approve.dao.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.plf.common.utils.StringUtil;
import com.plf.core.common.dao.OffersetTransformers;
import com.power.sifa.approve.model.Apl_admreview;
import com.power.sifa.approve.model.Aprv_process_materials;
import com.power.sifa.approve.model.pageInfo.Aprv_process_materialsPageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.query.internal.NativeQueryImpl;
import org.springframework.stereotype.Repository;
import com.power.sifa.approve.dao.Aprv_process_materialsDaoCustom;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* 审批过程材料 持久层实现(hibernate)
*/
@Slf4j
@Repository
public class Aprv_process_materialsDaoImpl implements Aprv_process_materialsDaoCustom{

    private final EntityManager entityManager;

    public Aprv_process_materialsDaoImpl(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public List getMaterialList(Aprv_process_materialsPageInfo pageInfo) {
        List<Aprv_process_materials> result = new ArrayList<>();

        StringBuffer sql = new StringBuffer("select a.*, b.targetname, c.acceptcode from aprv_process_materials a left join apl_admreview b on a.applyid = b.id  left join apl_main c on a.applyid = c.id where 1=1 ");
        List<Object> paramsList = new ArrayList<>();
        int index = 1;

        // 连查条件
        if(StringUtils.isNotEmpty(pageInfo.getF_targetname()) && StringUtils.isNotEmpty(pageInfo.getF_acceptcode())) {
            sql.append(" and (b.targetname like ?" + index++);
            paramsList.add("%" + pageInfo.getF_targetname() + "%");
            sql.append(" or c.acceptcode like ?" + index++ + ")");
            paramsList.add("%" + pageInfo.getF_acceptcode() + "%");
        } else {
            // 被申请人
            if (StringUtils.isNotEmpty(pageInfo.getF_targetname())) {
                sql.append(" and b.targetname like ?" + index++);
                paramsList.add("%" + pageInfo.getF_targetname() + "%");
            }
            // 案号
            if (StringUtils.isNotEmpty(pageInfo.getF_acceptcode())) {
                sql.append(" or c.acceptcode like ?" + index++);
                paramsList.add("%" + pageInfo.getF_acceptcode() + "%");
            }
        }


        // 文书类型
        if (ObjectUtil.isNotNull(pageInfo.getF_mattype())) {
            sql.append(" and a.mattype in ?" + index++);
            paramsList.add(pageInfo.getF_mattype());
        }

        // 排序
        sql.append(" order by a.createtime desc");
        Query query = entityManager.createNativeQuery(sql.toString());
        query.unwrap(NativeQueryImpl.class).setResultTransformer(OffersetTransformers.ALIAS_TO_ENTITY_MAP);
        log.info(sql.toString());

        // 添加条件
        for (int i = 0; i <= paramsList.size() - 1; i++) {
            query.setParameter(i + 1, paramsList.get(i));
        }
        // 查询总数
        pageInfo.setAllcount(query.getResultList().size());
        // 分页
        if (pageInfo.getAllcount() > 0) {
            int pageStart = (pageInfo.getPageNumber() - 1) * pageInfo.getPageSize();
            List<Map> list = query.setFirstResult(pageStart).setMaxResults(pageInfo.getPageSize()).getResultList();
            if (CollectionUtil.isNotEmpty(list)) {
                result = JSON.parseArray(JSON.toJSONString(list), Aprv_process_materials.class);
            }
        }
        return result;
    }
}
